<?php
/**
 *
 * Model for the run
 * @author Simon, Denis, Dennis
 *
 */
class Run_model extends CI_Model {
	/**
	 *
	 * Constructor
	 */
	function Run_model()
	{
		$this->load->database();
	}
	/**
	 *
	 * gets a run by ID
	 * @param  $runId ID of the run
	 */
	function getRunByID($runId)
	{
		//Query the data table for every record and row

		$query = $this->db->get_where('run', array('ID' => $runId));
			
		if ($query->num_rows() < 1)
		{
			//show_error('Database is empty!');
		}else{
			return $query->row();
		}
	}
	/**
	 *
	 * get all runs of a user
	 * @param  $userId ID of the user
	 */
	function getRunsFromUser($userId)
	{
		//Query the data table for every record and row
		$this->db->order_by("StartTime", "desc");
		$query = $this->db->get_where('run', array('User' => $userId));

		if ($query->num_rows() < 1)
		{
			//show_error('Database is empty!');
		}else{
			return $query->result();
		}
	}
	/**
	 *
	 * Get runs from one user on a route
	 * @param  $userId User ID
	 * @param  $routeID Route ID
	 */
	function getRunsFromUserAndRoute($userId, $routeID)
	{
		//Query the data table for every record and row
		$query = $this->db->get_where('run', array('User' => $userId, 'Route' => $routeID));

		if ($query->num_rows() < 1)
		{
			//show_error('Database is empty!');
		}else{
			return $query->result();
		}
	}
	/**
	 *
	 * Adds a run to the db
	 * @param  $userID User ID
	 * @param  $route Route
	 * @param  $startTime Starttime
	 * @param  $avgSpeed Average Speed
	 * @param  $avgPulse Average Pulse
	 * @param  $shoe Shoe
	 * @param  $calorieConsumption Calorie consumption
	 * @param  $duration Duration
	 * @param  $comment Comment
	 */
	function addRun($userID, $route, $startTime, $avgSpeed, $avgPulse, $shoe, $calorieConsumption, $duration, $comment )
	{
		$data = array(
			   'User' =>$userID,
			   'Route' => $route,
			   'StartTime' => $startTime,
               'AvgSpeed' => $avgSpeed,
               'AvgPulse' => $avgPulse,
               'Shoe' => $shoe,
               'CalorieConsumption' => $calorieConsumption,
               'Duration' => $avgPulse,
               'Comment' => $comment

		);

		return $this->db->insert('run', $data);
			
	}

	/**
	 *
	 * Updates a run to the db
	 * @param  runID Run ID
	 * @param  routeID Route ID
	 * @param  $startTime Starttime
	 * @param  $avgSpeed Average Speed
	 * @param  $avgPulse Average Pulse
	 * @param  $shoe Shoe
	 * @param  $calorieConsumption Calorie consumption
	 * @param  $duration Duration
	 * @param  $comment Comment
	 */
	function updateRun($runID, $routeID, $startTime, $avgSpeed, $avgPulse, $shoe, $calorieConsumption, $duration, $comment )
	{
		$data = array(
        			   'Route' => $routeID,
        			   'StartTime' => $startTime,
                       'AvgSpeed' => $avgSpeed,
                       'AvgPulse' => $avgPulse,
                       'Shoe' => $shoe,
                       'CalorieConsumption' => $calorieConsumption,
                       'Duration' => $avgPulse,
                       'Comment' => $comment

		);

		$this->db->where('ID', $runID);
		return $this->db->update('run', $data);

	}

	/**
	 * Delete a run by
	 * @param  $runID ID of the run
	 */
	function delRun($runID){
		$this->db->delete('run', array('ID' => $runID));
	}
}
?>